package com.qfedu.meidijiaju.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.meidijiaju.common.ResponseData;
import com.qfedu.meidijiaju.entity.*;
import com.qfedu.meidijiaju.mapper.CardYxyMapper;
import com.qfedu.meidijiaju.mapper.CommentsYxyMapper;
import com.qfedu.meidijiaju.service.CardYxyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author meidimeiju
 * @since 2022-10-09
 */
@Service
public class CardYxyServiceImpl extends ServiceImpl<CardYxyMapper, CardYxy> implements CardYxyService {

    @Autowired
    private CardYxyMapper cardYxyMapper;

    @Autowired
    private CommentsYxyMapper commentsYxyMapper;

    @Override
    public ResponseData<List<CardYxy>> allCard(int pagenum, int pagesize, int typeId) {

        ResponseData<List<CardYxy>> responseData = new ResponseData<>();

        List<CardYxy> cards = cardYxyMapper.allCard(typeId);

        if (cards != null && cards.size() > 0){

            for (CardYxy card : cards) {

                List<CommentsYxy> commentsYxyList = commentsYxyMapper.findCommentsByCardId(card.getId());

                int comments = commentsYxyList.size();

                int result = cardYxyMapper.updateCommentNum(comments,card.getId());

                if (result != 1){

                    responseData.setCode(0);

                    responseData.setMsg("运行修改评论数时有异常！！！");

                    return responseData;
                }

            }
        } else {

            responseData.setCode(0);

            responseData.setMsg("运行时有异常！！！");

            return responseData;

        }

        PageHelper.startPage(pagenum, pagesize);

        List<CardYxy> cardYxyList = cardYxyMapper.allCard(typeId);

        if (cardYxyList != null && cardYxyList.size() > 0) {

            for (CardYxy card : cardYxyList) {

                List<TitletagsYxy> titleTagList = card.getTitleTagList();

                if (titleTagList != null && titleTagList.size() > 0) {

                    List<String> titleTagStrs = new ArrayList<>();

                    for (TitletagsYxy titletagsYxy : titleTagList) {

                        String titleTagsName = titletagsYxy.getTitleTagsName();

                        titleTagStrs.add(titleTagsName);

                    }
                    card.setTitleTags(titleTagStrs);
                }

                List<ImgsYxy> imgList = card.getImgList();

                if (imgList != null && imgList.size() > 0) {

                    List<String> imgStrs = new ArrayList<>();

                    for (ImgsYxy imgsYxy : imgList) {
                        String img = imgsYxy.getImg();

                        imgStrs.add(img);

                    }
                    card.setImgs(imgStrs);

                }


                List<ContenttagsYxy> contentTagList = card.getContentTagList();

                if (contentTagList != null && contentTagList.size() > 0) {

                    List<String> contentTagStrs = new ArrayList<>();


                    for (ContenttagsYxy contenttagsYxy : contentTagList) {
                        String contentTagsName = contenttagsYxy.getContentTagsName();

                        contentTagStrs.add(contentTagsName);

                    }
                    card.setContentTags(contentTagStrs);

                }

                List<VideosYxy> videosYxyList = card.getVideoYxyList();

                if (videosYxyList != null && videosYxyList.size() > 0) {

                    List<String> videoStrs = new ArrayList<>();


                    for (VideosYxy videosYxy : videosYxyList) {
                        String video = videosYxy.getVideo();

                        videoStrs.add(video);

                    }
                    card.setVideos(videoStrs);

                }


            }

            PageInfo<CardYxy> pageInfo = new PageInfo<>(cardYxyList);

            int count = (int) pageInfo.getTotal();

            responseData.setCount(count);

            responseData.setCode(1);

            responseData.setData(cardYxyList);


        } else {

            responseData.setCode(0);

            responseData.setMsg("无数据！");

        }
        return responseData;
    }

    @Override
    public ResponseData<CardYxy> findCardById(int id) {
        ResponseData<CardYxy> responseData = new ResponseData<>();


        CardYxy card = cardYxyMapper.findCardById(id);

        if (card != null) {

            List<TitletagsYxy> titleTagList = card.getTitleTagList();

            if (titleTagList != null && titleTagList.size() > 0) {

                List<String> titleTagStrs = new ArrayList<>();

                for (TitletagsYxy titletagsYxy : titleTagList) {

                    String titleTagsName = titletagsYxy.getTitleTagsName();

                    titleTagStrs.add(titleTagsName);

                }
                card.setTitleTags(titleTagStrs);
            }

            List<ImgsYxy> imgList = card.getImgList();

            if (imgList != null && imgList.size() > 0) {

                List<String> imgStrs = new ArrayList<>();

                for (ImgsYxy imgsYxy : imgList) {
                    String img = imgsYxy.getImg();

                    imgStrs.add(img);

                }
                card.setImgs(imgStrs);

            }


            List<ContenttagsYxy> contentTagList = card.getContentTagList();

            if (contentTagList != null && contentTagList.size() > 0) {

                List<String> contentTagStrs = new ArrayList<>();


                for (ContenttagsYxy contenttagsYxy : contentTagList) {
                    String contentTagsName = contenttagsYxy.getContentTagsName();

                    contentTagStrs.add(contentTagsName);

                }
                card.setContentTags(contentTagStrs);

            }

            List<VideosYxy> videosYxyList = card.getVideoYxyList();

            if (videosYxyList != null && videosYxyList.size() > 0) {

                List<String> videoStrs = new ArrayList<>();


                for (VideosYxy videosYxy : videosYxyList) {
                    String video = videosYxy.getVideo();

                    videoStrs.add(video);

                }
                card.setVideos(videoStrs);

            }


//            PageInfo<CardYxy> pageInfo = new PageInfo<>(cards);
//
//            int count = (int) pageInfo.getTotal();
//
//            responseData.setCount(count);

            responseData.setCode(1);

            responseData.setData(card);


        } else {

            responseData.setCode(0);

            responseData.setMsg("无数据！");

        }
        return responseData;
    }
}
